package com.snbc.demo;

import java.io.IOException;
import java.net.SocketTimeoutException;

import com.snbc.barcode.sdk.demo.R;
import com.snbc.demo.MainActivity;
import com.snbc.demo.General.AlertDialogUtil;
import com.snbc.demo.General.SNBCApplication;
import com.snbc.sdk.barcode.BarInstructionImpl.BarPrinter;
import com.snbc.sdk.barcode.IBarInstruction.ILabelQuery;
import com.snbc.sdk.barcode.enumeration.InstructionType;
import com.snbc.sdk.connect.IConnect.DeviceConnect;
import com.snbc.sdk.connect.connectImpl.USBConnect;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class QueryPrinterStatusActivity extends Activity {
	private TextView tv_back;
	private Button btn_query_printer_status;
	private TextView tv_status_result;
	private SNBCApplication application;
	@SuppressLint("CutPasteId")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_query_printer_status);
//		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
//		.detectDiskReads().detectDiskWrites().detectNetwork()
//		.penaltyLog().build());
//
//		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
//		.detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()
//		.build());
		application = (SNBCApplication)getApplication();
		tv_back = (TextView) findViewById(R.id.tv_back_printer_status);
		btn_query_printer_status = (Button) findViewById(R.id.btn_query_printer_status);
		tv_status_result = (TextView) findViewById(R.id.tv_printer_status);
		tv_back.setOnClickListener(new BackListener());
		btn_query_printer_status.setOnClickListener(new QueryListener());
	
	}
	
	private class BackListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(QueryPrinterStatusActivity.this, MainActivity.class);
			startActivity(intent);
		}
		
	}
	
	/// \if English
	/// \defgroup QUERY_STATUS Query printer status
	/// \elseif Chinese
	/// \defgroup QUERY_STATUS 查询打印机状态
	/// \endif
	/// \code
	private class QueryListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			BarPrinter printer = null;
			InstructionType mType = null;
			StringBuilder buffer = new StringBuilder();
			try {
				printer = application.getPrinter();
				mType = printer.labelQuery().getPrinterLanguage();
				SNBCApplication application =  ((SNBCApplication)getApplication());
				switch (mType) {
				case BPLZ:
					AlertDialogUtil.showDialog("Sorry, BPLZ instruction can't support this function.", QueryPrinterStatusActivity.this);
					break;
				case BPLE:
					AlertDialogUtil.showDialog("Sorry, BPLE instruction can't support this function.", QueryPrinterStatusActivity.this);
					break;
				case BPLC:
					DeviceConnect connect = application.getConnect();
					String pt_str;
					int prt_st = 0;	
					int prt_str = 0;
					byte pszCommand[] = {0x1D,0x61,0x0f};
					byte[] recbuf = new byte[64];
					int readBytes = 0;
					
					try {
						//发送查询状态指令
						connect.write(pszCommand);
						
						//查询状态
						readBytes = connect.read(recbuf);
					} catch (SocketTimeoutException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(readBytes <= 0){	
						 prt_st = 0;
						 prt_str = 0;
					}
					
					try {
						//发送查询状态指令
						connect.write(pszCommand);
						
						//查询状态
						readBytes = connect.read(recbuf);
					} catch (SocketTimeoutException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(readBytes <= 0){	
						 prt_st = 0;
						 prt_str = 0;
					}	
					//判断打印机是否缺纸
					if((recbuf[2] & 0x0C) == 0x0C){
						prt_st = 1;
					}

					//判断打印机仓盖是否打开
					if((recbuf[0] & 0x20) == 0x20){
						prt_str = 1;
					}
					buffer.append(getResources().getString(R.string.hint_ready_to_print)).append((prt_str == 1) || (prt_st == 1)?"NO":"YES").append("\r\n");
					buffer.append(getResources().getString(R.string.hint_paper_out)).append(prt_st == 1?"YES":"NO").append("\r\n");
					buffer.append(getResources().getString(R.string.hint_head_opened)).append(prt_str == 1?"YES":"NO").append("\r\n");
					
					/*BarPrinter printer = application.getPrinter();
					ILabelQuery labelQuery = printer.labelQuery();
					if(labelQuery.getPrinterLanguage() == InstructionType.BPLA && !(application.getConnect() instanceof USBConnect)){
						AlertDialogUtil.showDialog("Sorry, BPLA instruction can't support this function.", QueryPrinterStatusActivity.this);
						return;
					}
					//buffer.append(getResources().getString(R.string.hint_ready_to_print)).append(labelQuery.getPrinterStatus()==0?" YES":" NO").append("\r\n");
					buffer.append(getResources().getString(R.string.hint_ready_to_print)).append(labelQuery.isReadyToPrint()==true?"YES":"NO").append("\r\n");
					buffer.append(getResources().getString(R.string.hint_paused)).append(labelQuery.isPaused()==true?"YES":"NO").append("\r\n");
					buffer.append(getResources().getString(R.string.hint_paper_out)).append(labelQuery.isPaperOut()==true?"YES":"NO").append("\r\n");
					buffer.append(getResources().getString(R.string.hint_head_too_hot)).append(labelQuery.isHeadTooHot()==true?"YES":"NO").append("\r\n");
					buffer.append(getResources().getString(R.string.hint_head_opened)).append(labelQuery.isHeadOpened()==true?"YES":"NO").append("\r\n");
					buffer.append(getResources().getString(R.string.hint_ribbon_out)).append(labelQuery.isRibbonOut()==true?"YES":"NO").append("\r\n");*/
					
					tv_status_result.setText(buffer.toString());
					break;
				case BPLT:
					AlertDialogUtil.showDialog("Sorry, BPLT instruction can't support this function.", QueryPrinterStatusActivity.this);
					break;
				case BPLA:
					AlertDialogUtil.showDialog("Sorry, BPLA instruction can't support this function.", QueryPrinterStatusActivity.this);
					break;
				default:
					break;
				}

			} catch (Exception e) {
				e.printStackTrace();
				AlertDialogUtil.showDialog(e, QueryPrinterStatusActivity.this);
				buffer = new StringBuilder(getResources().getString(R.string.hint_query_status_failed));
			}			
		}
		
	}
	/// \endcode
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
